import 'dart:io';

import 'package:extended_image/extended_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

enum ImageWidgetType { asset, network, file }

/// 图片组件
class ImageWidget extends StatelessWidget {
  /// 网址
  final String url;

  /// 类型
  final ImageWidgetType type;

  /// 圆角
  final double? radius;

  /// 宽度
  final double? width;

  /// 高度
  final double? height;

  /// 自适应方式
  final BoxFit? fit;

  /// 占位图
  final Widget? placeholder;

  /// 背景颜色
  final Color? backgroundColor;

  final int? cacheWidth;

  final bool? gaplessPlayback;

  /// 是否开启OSS图片缩放
  final bool? enableOSSResize;

  /// builder 函数
  final Widget Function(
    BuildContext context,
    ImageProvider provider,
    Widget completed,
    Size? size,
  )? builder;

  const ImageWidget(
      {Key? key,
      required this.type,
      required this.url,
      this.radius,
      this.width,
      this.height,
      this.fit,
      this.placeholder,
      this.backgroundColor,
      this.gaplessPlayback,
      this.builder,
      this.cacheWidth,
      this.enableOSSResize})
      : super(key: key);

  const ImageWidget.url(this.url,
      {Key? key,
      this.radius,
      this.width,
      this.height,
      this.fit,
      this.placeholder,
      this.backgroundColor,
      this.gaplessPlayback,
      this.builder,
      this.cacheWidth,
      this.enableOSSResize})
      : type = ImageWidgetType.network,
        super(key: key);

  const ImageWidget.asset(this.url,
      {Key? key,
      this.radius,
      this.width,
      this.height,
      this.fit,
      this.placeholder,
      this.backgroundColor,
      this.gaplessPlayback,
      this.builder,
      this.cacheWidth,
      this.enableOSSResize})
      : type = ImageWidgetType.asset,
        super(key: key);

  const ImageWidget.file(this.url,
      {Key? key,
      this.radius,
      this.width,
      this.height,
      this.fit,
      this.placeholder,
      this.backgroundColor,
      this.gaplessPlayback,
      this.builder,
      this.cacheWidth,
      this.enableOSSResize})
      : type = ImageWidgetType.file,
        super(key: key);

  Widget get _placeholder => placeholder ?? const ColoredBox(color: Colors.transparent);

  //     IconWidget.image(
  //   type == ImageWidgetType.asset ? AssetsImages.transparenPng : AssetsImages.defaultPng,
  //   size: 36,
  // );

  @override
  Widget build(BuildContext context) {
    final borderRadius = BorderRadius.all(
      Radius.circular(radius ?? 0),
    );
    Widget? image;
    switch (type) {
      case ImageWidgetType.asset:
        image = ExtendedImage.asset(
          url,
          width: width,
          height: height,
          fit: fit,
          shape: BoxShape.rectangle,
          borderRadius: borderRadius,
          gaplessPlayback: gaplessPlayback ?? true,
          loadStateChanged: (state) => _buildLoadState(context, state),
        );
        break;
      case ImageWidgetType.network:
        if (!url.contains('http')) break;
        String curUrl = url;
        if (enableOSSResize ?? false) {
          final double pixelRatio = MediaQuery.of(context).devicePixelRatio;
          var w = (pixelRatio * width!).toInt();
          var h = (pixelRatio * height!).toInt();
          curUrl = "$url?x-oss-process=image/resize,h_$h,w_$w";
        }
        image = ExtendedImage.network(
          curUrl,
          width: width,
          height: height,
          fit: fit,
          cacheWidth: cacheWidth,
          shape: BoxShape.rectangle,
          borderRadius: borderRadius,
          gaplessPlayback: gaplessPlayback ?? true,
          loadStateChanged: (state) => _buildLoadState(context, state),
        );
        break;
      case ImageWidgetType.file:
        image = ExtendedImage.file(
          File(url),
          width: width,
          height: height,
          fit: fit,
          shape: BoxShape.rectangle,
          borderRadius: borderRadius,
          gaplessPlayback: gaplessPlayback ?? true,
          loadStateChanged: (state) => _buildLoadState(context, state),
        );
    }

    return Container(
      clipBehavior: Clip.antiAlias,
      width: width,
      height: height,
      decoration: BoxDecoration(
        color: backgroundColor,
        borderRadius: borderRadius,
      ),
      child: image ?? _placeholder,
    );
  }

  Widget _buildLoadState(BuildContext context, ExtendedImageState state) {
    switch (state.extendedImageLoadState) {
      // 正在读取
      case LoadState.loading:
        return _placeholder;
      // 读取成功
      case LoadState.completed:
        Size? size;
        if (state.extendedImageInfo != null) {
          size = Size(
            state.extendedImageInfo!.image.width.toDouble(),
            state.extendedImageInfo!.image.height.toDouble(),
          );
        }
        final provider = state.imageProvider;
        final completed = state.completedWidget;
        return builder?.call(context, provider, completed, size) ?? completed;
      // 读取失败
      case LoadState.failed:
        return _placeholder;
    }
  }
}
